/*
 * Copyright 2009 Vladimir Ritz Bossicard
 *
 * This file is part of OraDoc.
 *
 * OraDoc is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Version     : $Revision: 20 $
 * Last edit   : $Date: 2009-03-25 12:43:30 +0000 (Wed, 25 Mar 2009) $
 * Last editor : $Author: vbossica $
 */
package org.oradoc.outputters;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.oradoc.utils.freemarker.HtmlTableOrganizer;

import freemarker.template.Configuration;

/**
 *
 * @author Vladimir Ritz Bossicard
 */
public class AbstractHtmlOutputter {

    protected final static int COLUMNS = 3;

    protected File outDir = null;

    protected Configuration cfg;

    /**
     * Sets the directory where generated files will be saved.
     *
     * @param outDir output directory
     * @throws IllegalArgumentException if the directory doesn't exist.
     */
    public void setOutputDir(final File outDir) {
        if (!outDir.exists()) {
            throw new IllegalArgumentException("output directory must exist: '" + outDir + "'");
        }
        this.outDir = outDir;
    }

    public void setConfiguration(final Configuration cfg) {
        this.cfg = cfg;
    }

    protected String[] orderNames(final List<String> input) {
        HtmlTableOrganizer organizer = new HtmlTableOrganizer(input.size(), COLUMNS);
        String[] names = new String[organizer.getTableSize()];
        for (int i = 0; i < names.length; i++) {
            names[i] = "";
        }
        for (int i = 0; i < input.size(); i++) {
            int pos = organizer.calculatePosition(i);
            names[pos] = input.get(i);
        }
        return names;
    }

    protected void copyCommonFiles() throws Exception {
        IOUtils.copy(this.getClass().getClassLoader().getResourceAsStream("resources/oradoc.css"), new FileOutputStream(new File(this.outDir, "oradoc.css")));
        File imagesDir = new File(this.outDir, "images");
        imagesDir.mkdirs();

        copyImage("alphabetical-active.gif", imagesDir);
        copyImage("alphabetical-inactive.gif", imagesDir);
        copyImage("main-page-active.gif", imagesDir);
        copyImage("main-page-inactive.gif", imagesDir);
        copyImage("table-list-active.gif", imagesDir);
        copyImage("table-list-inactive.gif", imagesDir);
        copyImage("tables-active.gif", imagesDir);
        copyImage("tables-inactive.gif", imagesDir);
        copyImage("view-list-active.gif", imagesDir);
        copyImage("view-list-inactive.gif", imagesDir);
        copyImage("views-active.gif", imagesDir);
        copyImage("views-inactive.gif", imagesDir);
    }

    private void copyImage(final String name, final File dir) throws Exception {
        IOUtils.copy(this.getClass().getClassLoader().getResourceAsStream("resources/images/" + name), new FileOutputStream(new File(dir, name)));
    }
}
